package com.lib.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lib.common.constant.CreateUpdateConstant;
import com.lib.mapper.LibraryMapper;
import com.lib.mapper.RecommendMapper;
import com.lib.pojo.DTO.RecommendAddBooksDTO;
import com.lib.pojo.DTO.RecommendPageDTO;
import com.lib.pojo.VO.RcommendVO;
import com.lib.pojo.VO.RecommendVO;
import com.lib.pojo.entity.PageResult;
import com.lib.pojo.entity.Recommend;
import com.lib.service.RecommendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class RecommendServiceImpl implements RecommendService {
    @Autowired
    private RecommendMapper recommendMapper;
    @Autowired
    private LibraryMapper libraryMapper;
    
    /**
     * 根据套餐ID查询图书
     *
     * @param recommendId
     * @return
     */
    @Override
    public List<RcommendVO> getRecommendById(Integer recommendId) {
        return recommendMapper.getRecommendById(recommendId);
    }
    
    /**
     * 根据套餐ID删除套餐
     * @param recommendId 套餐ID
     */
    @Override
    @Transactional
    public void deleteById(Integer recommendId) {
        //先查询这个套餐下面是不是还有图书关联
        List<RcommendVO> rcommendVOS = recommendMapper.getRecommendById(recommendId);
        if (rcommendVOS.size() > 0) {
            //删除跟套餐关联的图书的id 关系
            libraryMapper.deleteByRecommendId(recommendId);
        }
        recommendMapper.deleteById(recommendId);
    }
    
    /**
     * 根据套餐ID查询套餐信息
     *
     * @param recommendId 套餐ID
     * @return 套餐信息
     */
   public List<RcommendVO> getRecommendInfoById(Integer recommendId) {
        return recommendMapper.getRecommendInfoById(recommendId);
    }


    /**
     * 启用、禁用套餐
     * @param status 状态
     * @param recommendId 套餐ID
     */

    @Override
    public void statusStartOrStop(Integer status, Integer recommendId) {
        //创建对象recommend
        Recommend recommend = Recommend.builder()
                .status(status)
                .recommendId(recommendId)
                .updateTime(LocalDateTime.now())
                .updateUser(CreateUpdateConstant.UPDATE_USER)
                .build();
        recommendMapper.putRecommend(recommend);
    }

    /**
     * 新增套餐
     * @param recommendAddBooksDTO 套餐信息
     */
    @Override
    @Transactional()
    public void addRecommend(RecommendAddBooksDTO recommendAddBooksDTO) {

        //创建对象recommend
        Recommend recommend = Recommend.builder()
                .recommendId(recommendAddBooksDTO.getRecommendId())
                .recommendName(recommendAddBooksDTO.getRecommendName())
                .image(recommendAddBooksDTO.getImage())
                .description(recommendAddBooksDTO.getDescription())
                .status(recommendAddBooksDTO.getStatus())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .createUser(CreateUpdateConstant.CREATE_USER)
                .updateUser(CreateUpdateConstant.UPDATE_USER)
                .build();
        recommendMapper.addRecommend(recommend);
        Integer recommendId = recommend.getRecommendId();
        //添加图书和套餐的关联
        for (Integer bookId : recommendAddBooksDTO.getBookIds()) {
            libraryMapper.addDishAndSetmeal(bookId,recommendId);
        }
    }

    /**
     * 修改套餐
     * @param recommendAddBooksDTO 套餐信息
     */
    @Override
    @Transactional
    public void putRecommend(RecommendAddBooksDTO recommendAddBooksDTO) {
        //先删除这个套餐关联的图书的id 关系
        libraryMapper.deleteByRecommendId(recommendAddBooksDTO.getRecommendId());
        for (Integer bookId : recommendAddBooksDTO.getBookIds()) {
            libraryMapper.addDishAndSetmeal(bookId,recommendAddBooksDTO.getRecommendId());
        }
        //创建对象recommend
        Recommend recommend = Recommend.builder()
                .recommendId(recommendAddBooksDTO.getRecommendId())
                .recommendName(recommendAddBooksDTO.getRecommendName())
                .image(recommendAddBooksDTO.getImage())
                .description(recommendAddBooksDTO.getDescription())
                .status(recommendAddBooksDTO.getStatus())
                .updateTime(LocalDateTime.now())
                .updateUser(CreateUpdateConstant.UPDATE_USER)
                .build();
                recommendMapper.putRecommend(recommend);
    }

    //套餐分页查询
    @Override
    public PageResult page(RecommendPageDTO recommendPageDTO) {
        PageHelper.startPage(recommendPageDTO.getPage(),recommendPageDTO.getPageSize());
        if (recommendPageDTO.getRecommendId() != null){
            //只查询指定套餐下的图书
            Page<RecommendVO> page = recommendMapper.page(recommendPageDTO);
            long total = page.getTotal();
            List<RecommendVO> records = page.getResult();
            return new PageResult(total,records);
        }
        //查询所有
        Page<RecommendVO> page = recommendMapper.pageAll(recommendPageDTO);
        long total = page.getTotal();
        List<RecommendVO> records = page.getResult();
        return new PageResult(total,records);
    }

    //批量删除
    @Override
    public void delete(List<Integer> ids) {

        //判断这个套餐关联的是否有图书，有图书就不能删除
        for (Integer id : ids) {
            List<RcommendVO> rcommendVOS = recommendMapper.getRecommendById(id);
            if (rcommendVOS.size() > 0) {
                //删除跟套餐关联的图书的id 关系
                libraryMapper.deleteByRecommendId(id);
            }
        }
        for (Integer id : ids){
            recommendMapper.deleteById(id);
        }
    }

    //删除关联的图书id和套餐id
    @Override
    public void deleteByRecommendId(Integer recommendId) {
        libraryMapper.deleteRecommendById(recommendId);
    }


}
